<?php

namespace Core\Base;

/**
 * Description of Controller
 *
 * @author Admin
 */
class Controller
{
	protected $__layout = '';
	
	protected $__view = array();
	
	protected $__isRendered = false;


	protected $__layoutVars = array();
	
	/**
	 *
	 * @var \Core\Layout 
	 */
	protected $_layout;
	
	public function __construct()
	{
		$this->_layout = \App::layout();
	}
	
	public function __set($key, $val)
	{
		//$this->__layoutVars[$key] = $val;
		$this->_layout->assign($key, $val);
	}
	
	public function __get($key)
	{
		return $this->_layout->$key;
	}
	
	/**
	 * Befor dispatch layout
	 */
	protected function _preDispatch(){}
	
	/**
	 * Dispatch layout
	 */
	protected function _init()
	{
		if(!$this->__isRendered)
		{
			if(!$this->__layout)
				$this->__layout = \App::configs()->layouts['main'];
			
//			foreach($this->__layoutVars as $key => $val)
//			{
//				$this->_layout->assign($key, $val);
//			}
			
			$this->_layout->assign('configs', \App::configs());
			
			$this->__isRendered = true;
		}
	}
	
	/**
	 * After dispatch layout
	 */
	protected function _postDispatch()
	{}
	
	/**
	 * Render layout
	 */
	protected function render($view = null, $position = 'content')
	{
		if(!$view)
			$view = \App::routers()->getActionName();
		
		if(strpos($view, '/') === false)
			$view = strtolower(\App::routers()->getControllerName()) . '/' . $view;
		
		$this->__view[$position] = $view;
		
		$this->_preDispatch();
		$this->_init();
		$this->_postDispatch();
	}
	
	public function renderLayout()
	{
		foreach($this->__view as $position => $content)
		{
			$content = APPLICATION_PATH .'/modules/'. \App::routers()->getModuleName() .'/view/'. $content .\App::configs()->layouts['ext'];

			$this->__view[$position] = $this->_layout->fetch($content);
		}

		$this->_layout->assign('layout', (object)$this->__view);
		
		$this->_layout->display($this->__layout . \App::configs()->layouts['ext']);
	}

	protected function _getParam($name, $default = null)
	{
		if(isset($_GET[$name]))
			return $_GET[$name];
		
		if(isset($_POST[$name]))
			return $_POST[$name];
		
		return $default;
	}
	
	protected function _getParams()
	{
		return $_REQUEST;
	}
	
	protected function _getPost()
	{
		return $_POST;
	}

	protected function _isPost()
	{
		return count($_POST) > 0;
	}

	protected function _isXmlHttpRequest()
	{
		return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
	}
	
	protected function _redirect($url)
	{
		if(!$url)
			return false;
		
		header('Location: '. $url);
		
		return true;
	}
	
	protected function _forward($action = 'index', $controller = 'index', $module = 'default')
	{
		
		$controller = "\\Modules\\". $module .'\\'. $controller;
		$action = $action . 'Action';

		$obj = new $controller();

		$obj->$action();

		$obj->renderLayout();
		
		return true;
	}
	
	protected function _404()
	{
		header("HTTP/1.0 404 Not Found");
		header("Status: 404 Not Found");
		
		return $this->_forward('404', 'general', 'default');
	}
	
	protected function _cache($lifetime = 0)
	{
		if ($lifetime == 0)
		{
            // формуємо залоговок Expires
            header('Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
            // формуємо залоговок Cache-Control
            header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
            // формуємо залоговок Pragma
            header("Pragma: no-cache");
            return;
        }
		else 
		{
            header("Pragma: ");
			// формування повної дати в форматі GMT
			// кінечна дата життя кеша
			$date = gmdate("D, d M Y H:i:s", time() + $lifetime);
			// формуємо залоговок Expires
			header('Expires: '. $date . ' GMT' );
			// формуємо залоговок Cache-Control
			header('Cache-Control: public, max-age=' . $lifetime);
        }
	}
	
	protected function getIdByUrl($key = 'id')
	{
		$url = $this->_getParam($key, 0);

		return substr($url, 0, strpos($url, '-'));
	}
	
}
